// pages/user/withdraw.js
import request from '../../utils/request'
import WxNotificationCenter from '../../utils/WxNotificationCenter'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    balance: 0,
    choosenBank: '招商银行',
    cardNum: '',
    name: '',
    amount: '',
    banks: [
      '招商银行',
      '工商银行',
      '建设银行',
      '浦发银行',
      '农业银行',
      '民生银行',
      '兴业银行',
      '平安银行',
      '交通银行',
      '中信银行',
      '光大银行',
      '华夏银行',
      '中国银行',
      '广发银行',
      '北京银行',
      '宁波银行',
      '邮储银行'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    this.setData({
      id: id,
      balance: options.balance
    })
    this.hasWithdrawed(id, function(res) {
      if (res) {
        wx.showModal({
          title: '提示',
          content: '存在未完成的提现记录，请等待完成后再申请！',
          showCancel: false,
          success: function (res) {
            wx.navigateBack({})
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /*
   * 检查是否有未完成的提现
   */
  hasWithdrawed: function (id, callback) {
    let params = {
      album: this.data.id,
      status: 0
    }
    let url = app.globalData.apiHost + '/albums/withdraws/'
    request(url, 'GET', app.globalData.loginToken, params, function (err, data) {
      console.log(data)
      if (!err && data.count) {
        return callback(data)
      } else {
        return callback(null)
      }
    })
  },

  bindBankChange: function (e) {
    let index = e.detail.value
    this.setData({
      choosenBank: this.data.banks[index]
    })
  },

  cardNumChange: function (e) {
    this.setData({
      cardNum: e.detail.value
    })
  },

  nameChange: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  /*
   * 提现金额修改
   */
  amountChange: function (e) {
    this.setData({
      amount: e.detail.value
    })
  },

  /*
   * 全部提现
   */
  withdrawAll : function () {
    let balance = this.data.balance
    this.setData({
      amount: balance
    })
  },

  /*
   * 提交提现
   */
  confirmWithdraw: function() {
    let balance = parseFloat(this.data.balance)
    let amount = parseFloat(this.data.amount)
    let choosenBank = this.data.choosenBank
    let cardNum = this.data.cardNum
    let name = this.data.name
    if (isNaN(amount)) {
      return wx.showToast({
        title: '提现金额不正确',
        image: '/images/icon/icon_attention.png'
      })
    }
    if (amount <= 0 || amount > balance) {
      return wx.showToast({
        title: '提现金额不正确',
        image: '/images/icon/icon_attention.png'
      })
    }
    if (!choosenBank) {
      return wx.showToast({
        title: '请选择提现银行',
        image: '/images/icon/icon_attention.png'
      })
    }
    if (!cardNum) {
      return wx.showToast({
        title: '请输入银行卡号',
        image: '/images/icon/icon_attention.png'
      })
    }
    if (!name) {
      return wx.showToast({
        title: '请输入开户姓名',
        image: '/images/icon/icon_attention.png'
      })
    }
    
    let payAmount = (amount * 0.95).toFixed(1)
    let that = this
    wx.showModal({
      title: '提示',
      content: '提现将扣除5%手续费，实际到账金额为' + payAmount + '元。是否确定提现？',
      success: function (res) {
        if (res.confirm) {
          let formData = {
            amount: amount,
            bank: choosenBank,
            card: cardNum,
            name: name,
          }
          let url = app.globalData.apiHost + '/albums/' + that.data.id + '/withdraw/'
          request(url, 'POST', app.globalData.loginToken, formData, function (err, data) {
            console.log(data)
            if (!err) {
              wx.showModal({
                title: '提示',
                content: '申请提现成功，提现将在3~5个工作日到账',
                showCancel: false,
                success: function (res) {
                  let obj = { amount: amount }
                  WxNotificationCenter.postNotificationName('withdraw', obj)
                  wx.navigateBack({})
                }
              })
            }
          })
        }
      }
    })
  }

})